package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.log.BatteryLogDao;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Battery;
import com.tbit.main.pojo.BatteryLog;
import com.tbit.main.pojo.PowerChangeLogSta;
import com.tbit.main.service.*;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LMY
 * @create 2021-05-27 11:48
 * 电池操作日志
 */
@Service("batterLogService")
public class BatteryLogServiceImpl implements BatteryLogService {

    @Autowired
    private BatteryLogDao batteryLogDao;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private DataService dataService;
    @Autowired
    private BatteryService batteryService;
    @Autowired
    private RoleService roleService;

    @Override
    public void inserts(List<String> batteryNOs, Integer accountUserId, Integer model) {

        Integer accountId = dataService.getBatteryAccountId(batteryNOs.get(0));

        List<Battery> batteries = batteryService.getBatteryNOs(batteryNOs);

        batteryLogDao.inserts(batteries, accountId, accountUserId, model, DateTimeUtil.getNowTime());
    }

    @Override
    public void insert(String batteryNO, Integer accountUserId, Integer type, String remark) {

        Map<String, Object> map = new HashMap<>();

        if (dataService.getBatteryAccountId(batteryNO) == null) {
            map.put(DaoParamConstant.accountId, batteryService.getByNO(batteryNO).getAccountId());
            map.put("batteryId", batteryService.getByNO(batteryNO).getBatteryId());
        } else {
            map.put(DaoParamConstant.accountId, dataService.getBatteryAccountId(batteryNO));
            map.put("batteryId", dataService.getBatteryId(batteryNO));
        }
        map.put("accountUserId", accountUserId);
        map.put("type", type);
        map.put("batteryNO", batteryNO);
        map.put("addTime", DateTimeUtil.getNowTime());
        map.put("remark", remark);
        batteryLogDao.insert(map);
    }

    @Override
    public List<PowerChangeLogSta> groupByTime(Integer agentId, Integer userType, Integer usermainId, Integer type, String startTime, String endTime) {
        List<PowerChangeLogSta> dlses = new ArrayList<PowerChangeLogSta>();

        /**查询代理商运维账号*/
        List<Integer> accountUserIds = new ArrayList<Integer>();
        /**判断当前账号是不是运维人员*/
        if (roleService.getRoleCode(userType).equals(1)) {
            accountUserIds.add(usermainId);
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, agentId);
        params.put("accountUserId", StringUtil.getListString(accountUserIds));
        params.put("type", type);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);
        dlses = batteryLogDao.groupByTime(params);

        List<PowerChangeLogSta> dlsefilters = new ArrayList<PowerChangeLogSta>();
        if (dlses.size() > 0) {
            for (PowerChangeLogSta dls : dlses) {
                AccountUser au = accountUserService.getById(dls.getUserMainId());
                if (au != null) {
                    dls.setName(au.getName());
                    dls.setPhone(au.getPhone());

                    dlsefilters.add(dls);
                }
            }
        }

        return dlsefilters;
    }

    @Override
    public List<BatteryLog> getByTime(Integer accountId, Integer userMainId, Integer type, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("accountUserId", userMainId);
        params.put("type", type);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return batteryLogDao.getByTime(params);
    }

    @Override
    public void updateIsValid(String userCode, String batteryNO) {
        batteryLogDao.updateIsValid(userCode, batteryNO);
    }

    @Override
    public List<BatteryLog> bateryNO(Integer accountId, String batteryNO, String startTime, String endTime) {

        List<BatteryLog> batteryLogs = batteryLogDao.batteryNO(accountId, batteryNO, startTime, endTime);

        for (BatteryLog batteryLog : batteryLogs) {
            batteryLog.setAccountUserName(dataService.getAccountUser(batteryLog.getAccountUserId()) == null ? null : dataService.getAccountUser(batteryLog.getAccountUserId()).getName());
        }

        return batteryLogs;


    }

    @Override
    public List<BatteryLog> userId(Integer accountId, Integer accountUserId, String startTime, String endTime) {

        List<BatteryLog> batteryLogs = batteryLogDao.userId(accountId, accountUserId, startTime, endTime);

        batteryLogs.forEach(object -> object.setAccountUserName(dataService.getAccountUser(accountUserId) == null ? null : dataService.getAccountUser(accountUserId).getName()));

        return batteryLogs;
    }
}
